import { apiClients, tokenManager } from '@/services/apiClient';

export const autoLoginInDev = async (): Promise<void> => {
  if (process.env.NODE_ENV !== 'development') return;

  const currentToken = tokenManager.getToken();
  if (currentToken) return;

  try {
    const response = await apiClients.auth.authTokenCreate({
      tokenObtainPairRequest: {
        email: 'admin@example.com',  // 根据实际开发环境配置
        password: 'admin123'   // 根据实际开发环境配置
      }
    });

    // 🔑 关键：手动提取响应数据
    const tokenData = response.data;

    if (tokenData && tokenData.access) {
      tokenManager.setToken(tokenData.access);
      tokenManager.setRefreshToken(tokenData.refresh || '');
      console.log('🚀 开发环境自动登录成功');
    }
  } catch (error) {
    console.error('⚠️ 开发环境自动登录失败:', error);
  }
};
